﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
//using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using ClipboardLivePlugin;

namespace TestClipboard
{
    public partial class MainTestClipboardLive : Form
    {
        public MainTestClipboardLive()
        {
            InitializeComponent();
        }

        private void Bitmap2LocalButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            // Check to see if clipboard is an image file.
            if (data.GetDataPresent(DataFormats.Bitmap))
            {
                Bitmap screenCapture = (Bitmap)data.GetData(DataFormats.Bitmap);

                ImageClass imgProc = new ImageClass();
                string jpegDir = imgProc.GetTempJpegDirectory();
                if (File.Exists(jpegDir))
                    return;

                // We ae going to convert the image at 100% of original for right now.
                //  Why 100?  Who knows; guess I'm a perfectionist.  
                //  TODO: Put the quality they want the jpeg converted to into options.
                imgProc.saveBitmapAsJpeg(jpegDir, screenCapture, 100);
                content = @"<img src=""" + jpegDir + @""" />";
            }
        }

        private void Rtf2HtmlButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            if ((data.GetDataPresent(DataFormats.Rtf)) && (!data.GetDataPresent(DataFormats.Html)))
            {
                string textFromCB = (string)data.GetData(DataFormats.Rtf);

                HtmlRichTextBox txtBox = new HtmlRichTextBox();
                txtBox.Rtf = textFromCB;

                string html = txtBox.GetHTML(true, true);
                content = html;
            }
        }

        private void Rtf2TextButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            if ((data.GetDataPresent(DataFormats.Rtf)) && (!data.GetDataPresent(DataFormats.Html)))
            {
                string textFromCB = (string)data.GetData(DataFormats.Rtf);

                HtmlRichTextBox txtBox = new HtmlRichTextBox();
                txtBox.Rtf = textFromCB;

                string text = txtBox.Text;
                content = text;
            }

        }

        private void Html2FragmentButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(DataFormats.Html))
            {
                CLHtmlFragment html = CLHtmlFragment.FromClipboard();
                content = html.SpecialFragment(FragmentType.ImgParsedToHttp);
            }
        }

        private void HTML2FragmentLocalButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(DataFormats.Html))
            {
                CLHtmlFragment html = CLHtmlFragment.FromClipboard();
                content = html.SpecialFragment(FragmentType.ImgParsedToLocalFile);
            }
        }

        private void Html2TextButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(DataFormats.Html))
            {
                CLHtmlFragment html = CLHtmlFragment.FromClipboard();
                content = html.SpecialFragment(FragmentType.ParseAsText);
            }
        }

        private void TextButton_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            IDataObject data = new DataObject();
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(DataFormats.Text)
                || data.GetDataPresent(DataFormats.StringFormat)
                || data.GetDataPresent(DataFormats.CommaSeparatedValue))
            {
                string textFromCB = data.GetData(DataFormats.Text).ToString();
                content = textFromCB;
            }
        }


        #region Old Test - Not used anymore
        //public DialogResult TestConversion()
        //{
        //    string content = string.Empty;
        //    IDataObject data = new DataObject();
        //    data = Clipboard.GetDataObject();

        //    // Check to see if clipboard is an image file.
        //    if (data.GetDataPresent(DataFormats.Bitmap))
        //    {
        //        Bitmap screenCapture = (Bitmap)data.GetData(DataFormats.Bitmap);

        //        string jpegDir = GetTempJpegDirectory();
        //        if (File.Exists(jpegDir))
        //            return DialogResult.Cancel;

        //        saveJpeg(jpegDir, screenCapture, 80);
        //        content = @"<img src=""" + jpegDir + @""" />";
        //        return DialogResult.OK;
        //    }
        //    if (data.GetDataPresent(DataFormats.Rtf))
        //    {
        //        string textFromCB = (string)data.GetData(DataFormats.Rtf);

        //        HtmlRichText.HtmlRichTextBox txtBox = new HtmlRichText.HtmlRichTextBox();
        //        txtBox.Rtf = textFromCB;

        //        string html = txtBox.GetHTML(true, true);
        //        content = html;
        //        return DialogResult.OK;
        //    }
        //    if (data.GetDataPresent(DataFormats.Html))
        //    {
        //        string orginalHtml = (string)data.GetData(DataFormats.Text);
        //        HtmlFragment html = HtmlFragment.FromClipboard();
        //        content = html.PictureFormatedFragment();
        //        return DialogResult.OK;
        //    }
        //    if (data.GetDataPresent(DataFormats.Text)
        //        || data.GetDataPresent(DataFormats.StringFormat)
        //        || data.GetDataPresent(DataFormats.CommaSeparatedValue))
        //    {
        //        string textFromCB = (string)data.GetData(DataFormats.Text);
        //        content = textFromCB;
        //        return DialogResult.OK;
        //    }

        //    MessageBox.Show("The clipboard content is not recognized and cannot be pasted using this plugin.", "Clipboard paste error", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    return DialogResult.Cancel;
        //}
        #endregion


    

    
    }
}
